﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using LiteDB;

namespace PipeDetection.Repository.Base
{
    public class BaseRepository<TEntity> where TEntity : class, new()
    {
        private readonly string databasefile;
        private readonly string tablename;
        private BsonMapper mapper;
        public BaseRepository()
        {
            databasefile = BaseDBConfig.ConnectionString;
            var type = typeof(TEntity);
            tablename = type.Name;
            mapper = new BsonMapper();

        }


        public TEntity QueryByKey(long objId)
        {
            using (var db = new LiteDatabase(databasefile, mapper))
            {
                var col = db.GetCollection<TEntity>(tablename,BsonAutoId.Int32);
               return col.FindById(objId);
            }
        }

  

        /// <summary>
        ///     获取所有
        /// </summary>
        /// <returns></returns>
        public List<TEntity> Query()
        {
            using (var db = new LiteDatabase(databasefile, mapper))
            {
                var col = db.GetCollection<TEntity>(tablename, BsonAutoId.Int32);
                return col.Query().ToList();
            }
        }




        /// <summary>
        ///     根据Lamabada表达式获取
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public List<TEntity> Query(Expression< Func< TEntity, bool>> func)
        {
            using (var db = new LiteDatabase(databasefile, mapper))
            {
                var col = db.GetCollection<TEntity>(tablename, BsonAutoId.Int32);
                return col.Query().Where(func).ToList();
            }
        }


        /// <summary>
        ///     写入实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns>ID</returns>
        public long Add(TEntity entity)
        {
            using (var db = new LiteDatabase(databasefile, mapper))
            {
                var col = db.GetCollection<TEntity>(tablename, BsonAutoId.Int32);
                
                return col.Insert(entity).AsInt64;
            }
        }
        public void Add(List<TEntity> entities)
        {
            using (var db = new LiteDatabase(databasefile, mapper))
            {
                var col = db.GetCollection<TEntity>(tablename, BsonAutoId.Int32);
                entities.ForEach(x => col.Insert(x));
            }
        }
        /// <summary>
        ///     更新实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public bool Update( TEntity entity)
        {
            //这种方式会以主键为条件
            using (var db = new LiteDatabase(databasefile, mapper))
            {
                var col = db.GetCollection<TEntity>(tablename, BsonAutoId.Int32);
                return col.Update(entity);
             }
        }

        public void Update(List<TEntity> entities)
        {
            //这种方式会以主键为条件
            using (var db = new LiteDatabase(databasefile, mapper))
            {
                var col = db.GetCollection<TEntity>(tablename, BsonAutoId.Int32);
                entities.ForEach(x => col.Update(x));
            }
        }
        /// <summary>
        ///     删除指定ID的数据
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public bool DeleteByKey(int objkey)
        {
            using (var db = new LiteDatabase(databasefile, mapper))
            {
                var col = db.GetCollection<TEntity>(tablename, BsonAutoId.Int32);

               return col.Delete(objkey);
            }

            ;
        }
    }
}